package servlet;

import entity.Movie;
import service.MovieService;
import service.impl.MovieServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取表单数据
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String type=request.getParameter("type");
        String price=request.getParameter("price");
        String tuijian=request.getParameter("tuijian");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String votes=request.getParameter("votes");
        //封装数据到实体类
        Movie movie=new Movie();
        movie.setMovieId(Integer.valueOf(id));
        movie.setMovieName(name);
        movie.setMovieType(type);
        movie.setPrice(Float.valueOf(price));
        movie.setRecommend(Integer.valueOf(tuijian));
        movie.setStartTime(startTime);
        movie.setEndTime(endTime);
        movie.setVotes(Integer.valueOf(votes));
        //调用新增方法，保存数据到数据库
        MovieService movieService=new MovieServiceImpl();
        Integer count=movieService.upate(movie);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        if(count>0){
            out.print("<script>alert('修改成功');location.href='GetByPageServlet'</script>");
        }else{
            out.print("<script>alert('修改失败');history.go(-1)</script>");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
